/**  
 * @Project: jeecg
 * @Title: ProcessEndListener.java
 * @Package com.oa.manager.workFlow.listener.execution
 * @date 2013-8-16 下午2:04:12
 * @Copyright: 2013 
 */
package org.jeecg.modules.extbpm.listener.execution;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.extbpm.process.common.WorkFlowGlobals;



/**
 * 	类名：SubProcessStartListener
 * 	功能：会签子流程开始监听器
 * @author zhoujf
 *
 */
public class SubProcessHqStartListener implements ExecutionListener{

	private static final long serialVersionUID = 1L;
	
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		
		RuntimeService runtimeService = SpringContextUtils.getBean(RuntimeService.class);
		String mainProcessId = (String)execution.getVariable(WorkFlowGlobals.JG_SUB_MAIN_PROCESS_ID);
		String url = (String)execution.getVariable(WorkFlowGlobals.BPM_FORM_CONTENT_URL);
		String bizTitle = (String)runtimeService.getVariable(mainProcessId,WorkFlowGlobals.BPM_BIZ_TITLE);
		if(StringUtils.isEmpty(url)) {
			url = (String)runtimeService.getVariable(mainProcessId,WorkFlowGlobals.BPM_FORM_CONTENT_URL);
			String mobileUrl = (String)runtimeService.getVariable(mainProcessId,WorkFlowGlobals.BPM_FORM_CONTENT_URL_MOBILE);
			execution.setVariable(WorkFlowGlobals.BPM_FORM_CONTENT_URL, url);
			execution.setVariable(WorkFlowGlobals.BPM_FORM_CONTENT_URL_MOBILE, mobileUrl);
		}
		execution.setVariable(WorkFlowGlobals.BPM_BIZ_TITLE, bizTitle);
		
		//获取主表表名
		String tableName = (String)runtimeService.getVariable(mainProcessId,WorkFlowGlobals.BPM_FORM_KEY);
		execution.setVariable(WorkFlowGlobals.BPM_FORM_KEY, tableName);
		//获取主表数据id
		String businessKey = (String)runtimeService.getVariable(mainProcessId, WorkFlowGlobals.BPM_DATA_ID);
		execution.setVariable(WorkFlowGlobals.BPM_DATA_ID, businessKey);
		
		//子流程实例set业务号和主流程保持一致
        runtimeService.updateBusinessKey(execution.getProcessInstanceId(), businessKey);
	}
	
	
	

}
